/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  CommonUtil.java   
 * @Package com.axin.common.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年4月20日 下午10:51:29   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.common.utils;

import java.math.BigDecimal;

import com.axin.common.conf.Config;

/**   
 * @Description: TODO 
 * @ClassName:  CommonUtil
 * @author:  Axin 
 * @date:   2019年4月20日 下午10:51:29   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
public class JobUtil {

	
	/**
	 * 按档位计算每次购买的价格位置
	 * @Description: TODO  
	 * @Title: getPositions   
	 * @param basePrice
	 * @param buyLevel
	 * @param buyCountLimit
	 * @param scale
	 * @return String[]
	 * @throws
	 */
	public static String[] getPositions(String basePrice,String buyLevel,int buyCountLimit,int scale){
		//档位百分比
		String parcent = Config.BUY_LEVEL_VALUE.get(buyLevel);
		//基本单价
		BigDecimal basePriceBig = new BigDecimal(basePrice).setScale(scale, BigDecimal.ROUND_DOWN);
		//需要分单购买的价格段
		BigDecimal subPrice = basePriceBig.multiply(new BigDecimal(parcent)).setScale(scale, BigDecimal.ROUND_DOWN);
		
		//BigDecimal lastPrice = basePriceBig.subtract(subPrice).setScale(scale, BigDecimal.ROUND_DOWN);;
		//价格分割大小
		BigDecimal splitPrice = subPrice.divide(new BigDecimal(buyCountLimit),scale,BigDecimal.ROUND_DOWN);
		//声明一个数组，存放计算结果
		String[] positions = new String[buyCountLimit];
		for (int i = 0; i < positions.length; i++) {
			if(i == 0){
				positions[i] = basePriceBig.toPlainString();
			}else{
				basePriceBig = basePriceBig.subtract(splitPrice).setScale(scale, BigDecimal.ROUND_DOWN);
				positions[i] = basePriceBig.toPlainString();	
			}
		}
		
		return positions;
	}
	
	
	/**
	 * 获取策略金额
	 * @Description: TODO  
	 * @Title: getStrategyMoney   
	 * @param money
	 * @param count
	 * @param scale
	 * @param strategy
	 * @return String[]
	 * @throws
	 */
	public static String[] getStrategyMoney(String money,int count,int scale,String strategy){
		if("1".equals(strategy)){
			return getFibo(money,count,scale);
		}else if("2".equals(strategy)){
			return getSquare(money,count,scale,2);
		}else if("3".equals(strategy)){
			return getSquare(money,count,scale,3);
		}else{
			throw new RuntimeException("非法的策略参数 。");
		}
	}
	/**
	 * 计算斐波那契数列
	 * @Title: getFibo   
	 * @param money
	 * @param count
	 * @param scale
	 * @return String[]
	 * @throws
	 */
	private static String[] getFibo(String money,int count,int scale){
		String[] arr = new String[count];
		for (int i = 0; i < arr.length; i++) {
			BigDecimal num1 = null;
        	BigDecimal num2 = null;
        	if(i == 0){
        		num1 = new BigDecimal(0);
        		num2 = new BigDecimal(money);
			}else if(i == 1){
				num1 = num2 = new BigDecimal(money);
			}else{
				num1 = new BigDecimal(arr[i-1]);
	        	num2 = new BigDecimal(arr[i-2]);
			}
        	arr[i] = num1.add(num2).setScale(scale, BigDecimal.ROUND_DOWN).toPlainString();
        } 
		return arr;
		
	}
	
	/**
	 * 根据参数获取平方或立方数列
	 * @Description: TODO  
	 * @Title: getSquare   
	 * @param money
	 * @param count
	 * @param scale
	 * @param base 2平方 3立方
	 * @return String[]
	 * @throws
	 */
	private static String[] getSquare(String money,int count,int scale,int base){
		String[] arr = new String[count];
		BigDecimal baseBig = new BigDecimal(base);
		BigDecimal num = new BigDecimal(money).setScale(scale, BigDecimal.ROUND_DOWN);
		for (int i = 0; i < arr.length; i++) {
			if(i == 0){
				arr[i] = num.toPlainString();
			}else{
				BigDecimal cur = num.multiply(baseBig).setScale(scale, BigDecimal.ROUND_DOWN);
				num = cur;
				arr[i] = cur.toPlainString();
				
			}
        } 
		return arr;
	}
	
	
}
